﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace sg.edu.nus.iss.prettyfaces.DataAccessLayer
{
    public interface IModelRepository
    {
        PrettyModel ViewModelProfile(Guid id);
        IEnumerable<Message> getMessageById(Guid id);
        IEnumerable<PrettyModel> getAllModels();
        Message getDetailMessageById(Guid id);
        void createNewMessage(Message msg);
        IEnumerable<Relationship> getRequestById(Guid id);
        Relationship getSingleRequestById(Guid id);
        void Save();
    }

    public class ModelRepository : IModelRepository
    {
        PrettyFaceEntity context = new PrettyFaceEntity();
        public virtual PrettyModel ViewModelProfile(Guid id)
        {
            return context.PrettyModels.SingleOrDefault(m => m.ModelId == id);
        }

        //public virtual void SendMessage(Guid ID,Guid SenderUserId,Guid ReceiverUserId,string Text)
        //{
        //    context.SaveChanges();
        //}
        public virtual IEnumerable<Message> getMessageById(Guid id)
        {
            return context.Messages.Where(x => x.ReceiverUserId == id);           
        }

        public virtual IEnumerable<PrettyModel> getAllModels()
        {
            return context.PrettyModels;
        }
        public virtual Message getDetailMessageById(Guid id)
        {
            return context.Messages.SingleOrDefault(x => x.ID == id);
        }
        public virtual void createNewMessage(Message msg)
        {
            context.Messages.AddObject(msg);
        }
        public virtual IEnumerable<Relationship> getRequestById(Guid id)
        {
            return context.Relationships.Where(x => x.ModelID == id);
        }
        public virtual Relationship getSingleRequestById(Guid id)
        {
            return context.Relationships.SingleOrDefault(m => m.PhotographerID == id);
        }
        public virtual IEnumerable<aspnet_Users> getAllUsers()
        {
            return context.aspnet_Users.ToList();
        }
        public virtual string getUserNameById(Guid id)
        {
            aspnet_Users users=context.aspnet_Users.SingleOrDefault(m => m.UserId == id);
            return users.UserName;
        }
        public virtual Boolean UpdateStatus(Guid id, int status)
        {
            Relationship request = context.Relationships.SingleOrDefault(m => m.PhotographerID == id);
            request.Status = status;
            return true;
        }
        public virtual Relationship getRelationshipById(Guid id)
        {
            Relationship request = context.Relationships.SingleOrDefault(m => m.PhotographerID == id);
            return request;
        }
        public virtual IEnumerable<Relationship> getRequestByPId(Guid id)
        {
            return context.Relationships.Where(x => x.PhotographerID == id);
        }

        //Persistence
        public virtual void Save()
        {
            context.SaveChanges();
        }
    }
}